﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Excel;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using DevComponents.DotNetBar;

namespace project_assess.read_excel
{
    public partial class input_excel_frm : Office2007Form
    {
        string path1 = System.Windows.Forms.Application.StartupPath + @"\报表\项目测算表_temp.xls";
        read_Excel_frm rfrm;
        public input_excel_frm(read_Excel_frm xfrm)
        {
            InitializeComponent();
            string path;
            path = System.Windows.Forms.Application.StartupPath + @"\报表\项目测算表.xls";
            if (File.Exists(path1))
            {
                webBrowser1.Navigate(path1);
            }
            else
            {
                File.Copy(path, path1);
                webBrowser1.Navigate(path1);
            }
            object miss = Type.Missing;
            rfrm = xfrm;
        }
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);


        private void btn_new_Click(object sender, EventArgs e)
        {
            string path;
            path = System.Windows.Forms.Application.StartupPath + @"\报表\项目测算表.xls";

            object axWebBrowser = this.webBrowser1.ActiveXInstance;
            object Application = axWebBrowser.GetType().InvokeMember("Document",
             BindingFlags.GetProperty, null, axWebBrowser, null);

            Workbook wbb = Application as Workbook;
            if (wbb == null)
            {
                return;
            }
            Excel.ApplicationClass excel = wbb.Application as Excel.ApplicationClass;
            object miss = Type.Missing;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wbb);
            IntPtr t = new IntPtr(excel.Hwnd);
            int k = 0;
            GetWindowThreadProcessId(t, out k);
            Process p = Process.GetProcessById(k);
            p.Kill();
            Thread.Sleep(100);
            webBrowser1.Navigate(string.Empty);
            //webBrowser1.Navigate(path);
            try
            {
                if (File.Exists(path1))
                {
                    File.Delete(path1);
                }
                File.Copy(path, path1);

            }
            catch(Exception a)
            {
                MessageBox.Show(a.Message);
                return;
            }
            webBrowser1.Navigate(path1);
         
        }

        private void btnexport_Click(object sender, EventArgs e)
        {
            message_frm msg = new message_frm("是否要读入测算表?", "是否读入", icon_img.query);
            msg.ShowDialog(this);
   
            if (msg.yesorno == true)
            {
                object axWebBrowser = this.webBrowser1.ActiveXInstance;
                object Application = axWebBrowser.GetType().InvokeMember("Document",
                 BindingFlags.GetProperty, null, axWebBrowser, null);

                Workbook wbb = Application as Workbook;
                if (wbb == null)
                {
                    return;
                }
                Excel.ApplicationClass excel = wbb.Application as Excel.ApplicationClass;
                object miss = Type.Missing;
                wbb.Save();
                wbb.Close(true, wbb.FullName, miss);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wbb);

                IntPtr t = new IntPtr(excel.Hwnd);

                int k = 0;
                GetWindowThreadProcessId(t, out k);
                Process p = Process.GetProcessById(k);
                p.Kill();
                Thread.Sleep(100);

                msg.Close();
                rfrm.read_excel(path1, checkBox.Checked); 
                this.Close();
            }
            else
            {
                msg.Close();
            }
        }

        private void input_excel_frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            object axWebBrowser = this.webBrowser1.ActiveXInstance;
            object Application = axWebBrowser.GetType().InvokeMember("Document",
             BindingFlags.GetProperty, null, axWebBrowser, null);

            Workbook wbb = Application as Workbook;
            if (wbb == null)
            {
                return;
            }
            Excel.ApplicationClass excel = wbb.Application as Excel.ApplicationClass;
            object miss = Type.Missing;
            wbb.Save();
            wbb.Close(true, wbb.FullName, miss);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wbb);

            IntPtr t = new IntPtr(excel.Hwnd);

            int k = 0;
            GetWindowThreadProcessId(t, out k);
            Process p = Process.GetProcessById(k);
            p.Kill();
        }






    }
}
